package com.example.androidframework.ui.exchange

import android.view.Gravity
import android.view.ViewGroup.MarginLayoutParams
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import com.example.androidframework.R
import com.example.androidframework.databinding.FragmentExchangeBinding
import com.example.androidframework.ui.goods.GoodsDetailActivity
import com.example.androidframework.ui.login.LoginActivity
import com.martin.lib_base.LibBase
import com.martin.lib_base.base.BaseFragment
import com.martin.lib_base.bean.app.exchange.GoodsCategoryBean
import com.martin.lib_base.interfaces.IGetData
import com.martin.lib_base.interfaces.IOnItemClickListener
import com.martin.lib_base.ktx.addOnItemClickListener
import com.martin.lib_base.ktx.loadMore
import com.martin.lib_base.ktx.loge
import com.martin.lib_base.ktx.refresh
import com.martin.lib_base.ui.SpacesItemDecoration
import com.martin.lib_base.ui.dp
import dev.utils.app.BarUtils
import kotlinx.coroutines.launch

class ExchangeFragment : BaseFragment<FragmentExchangeBinding, ExchangeViewModel>() {

    override fun getViewModelClass(): Class<ExchangeViewModel> {
        return ExchangeViewModel::class.java
    }

    override fun layoutId(): Int {
        return R.layout.fragment_exchange
    }

    override fun initOnViewCreated() {
        // 设置导航栏距离顶部的距离
        setTopBarLayoutMarginTop()

        binding.viewModel = viewModel
        binding.pageBean = viewModel.pageModel

        // 请求分类数据
        viewModel.getCategoryData()
        viewModel.getData()

        // 刷新布局数据绑定
        viewModel.pageModel.bindSmartRefreshLayout(binding.srl, binding.rv, viewModel, this)
        // 点击事件
        binding.rv.addOnItemClickListener(object : IOnItemClickListener {
            override fun onItemClick(position: Int, type: Int) {
                loge("onItemClick position === $position")

                val bean = viewModel.mallData.value.goods_list?.get(position)
                bean?.let {
                    viewModel.goPage(it.id)
                }
            }

        })

        // 设置数据
        lifecycleScope.launch {
            viewModel.categoryList.collect {
                categoryTabs()
            }
        }

        lifecycleScope.launch {
            viewModel.mallData.collect {
                // 处理商品数据
                viewModel.pageModel.handleDataList(it.goods_list)

            }
        }
    }

    /// 设置导航栏距离顶部的距离
    private fun setTopBarLayoutMarginTop() {
        val params: MarginLayoutParams = binding.topBar.layoutParams as MarginLayoutParams
        // 设置顶部距离为StatusBar的高度
        params.topMargin = BarUtils.getStatusBarHeight()
        binding.topBar.layoutParams = params
    }

    /**
     * 分类标题
     */
    private fun categoryTabs() {
        viewModel.categoryList.value.let {
            binding.tabs.removeAllViews()
            val cates = it
            cates.add(0, GoodsCategoryBean().apply {
                gc_id = ""
                gc_name = "全部"
            })
            for (index in 0 until cates.size) {
                val bean = cates[index]
                val textView = TextView(context)
                textView.text = bean.gc_name
                textView.setPadding(
                    if (index == 0) 15 else 0,
                    0,
                    if (index == cates.size - 1) 0 else 50,
                    0
                )
                textView.gravity = Gravity.CENTER_VERTICAL
                binding.tabs.addView(textView)
            }

            binding.tabs.configTabLayoutConfig {
                // 切换事件
                onSelectIndexChange =
                    { fromIndex: Int, selectIndexList: List<Int>, reselect: Boolean, fromUser: Boolean ->
                        val position: Int = selectIndexList.first()
                        loge("onSelectIndexChange ------ fromIndex = $fromIndex, position = $position")
                        val bean = cates[position]
                        viewModel.categoryId.value = bean.gc_id
                        // 页面初始化时会从-1到0,且可能会调用多次
                        // 为避免接口多次调用,特做此处理
                        if (fromIndex != -1) {
                            viewModel.getData()
                        }
                    }
            }
        }

    }

    companion object {
        fun getInstance(): Fragment {
            return ExchangeFragment()
        }
    }

}